package com.yan.deezer.data.parser;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.yan.deezer.data.object.Album;
import com.yan.deezer.data.object.Artist;
import com.yan.deezer.data.object.Track;

public class ParsingManager extends BasicParser implements Tags {
	private static final String TAG = "DEEZER_YAN_PARSING_MANAGER";

	public static List<Artist> buildArtistList(String json) {

		Log.d(TAG, json);
		List<Artist> artistList = new ArrayList<Artist>();
		Artist artist;
		int startOffset = json.indexOf("[");
		try {
			JSONArray jsonArray = new JSONArray(json.substring(startOffset));
			int nbJSONObject = jsonArray.length();
			JSONObject jsonObject;
			for (int i = 0; i < nbJSONObject; ++i) {
				jsonObject = jsonArray.getJSONObject(i);
				artist = buildArtist(jsonObject);
				artistList.add(artist);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return artistList;
	}

	public static List<Album> buildArtistAlbumList(String json) {

		Log.d(TAG, json);
		List<Album> artistAlbumList = new ArrayList<Album>();
		Album album;
		int startOffset = json.indexOf("[");
		try {
			JSONArray jsonArray = new JSONArray(json.substring(startOffset));
			int nbJSONObject = jsonArray.length();
			JSONObject jsonObject;
			for (int i = 0; i < nbJSONObject; ++i) {
				jsonObject = jsonArray.getJSONObject(i);
				album = buildAlbum(jsonObject);
				artistAlbumList.add(album);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return artistAlbumList;
	}

	public static List<Track> buildAlbumTrackList(String json) {

		Log.d(TAG, json);
		List<Track> albumTrackList = new ArrayList<Track>();
		Track track;
		int startOffset = json.indexOf("[");
		try {
			JSONArray jsonArray = new JSONArray(json.substring(startOffset));
			int nbJSONObject = jsonArray.length();
			JSONObject jsonObject;
			for (int i = 0; i < nbJSONObject; ++i) {
				jsonObject = jsonArray.getJSONObject(i);
				track = buildTrack(jsonObject);
				albumTrackList.add(track);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return albumTrackList;
	}

	private static Artist buildArtist(JSONObject jsonObject) {

		Artist mArtist = new Artist();

		try {
			mArtist.setId(getString(jsonObject, ARTIST_ID));
			mArtist.setName(getString(jsonObject, ARTIST_NAME));
			mArtist.setLink(getString(jsonObject, ARTIST_LINK));
			mArtist.setPicture(getString(jsonObject, ARTIST_PICTURE));
			mArtist.setNb_album(getString(jsonObject, ARTIST_NB_ALBUM));
			mArtist.setNb_fan(getString(jsonObject, ARTIST_NB_FAN));
			mArtist.setRadio(getString(jsonObject, ARTIST_RADIO));
			mArtist.setType(getString(jsonObject, ARTIST_TYPE));
		}

		catch (Exception e)

		{
			e.printStackTrace();

			return null;

		}

		return mArtist;

	}

	private static Album buildAlbum(JSONObject jsonObject) {

		Album album = new Album();

		try {
			album.setId(getString(jsonObject, ALBUM_ID));
			album.setArtistString(getString(jsonObject, ALBUM_ARTIST));
			album.setLink(getString(jsonObject, ALBUM_LINK));
			album.setCover(getString(jsonObject, ALBUM_COVER));
			album.setDuration(getString(jsonObject, ALBUM_DURATION));
			album.setFans(getString(jsonObject, ALBUM_FANS));
			album.setLabel(getString(jsonObject, ALBUM_LABEL));
			album.setTitle(getString(jsonObject, Tags.ALBUM_TITLE));
			album.setRelease_date(getString(jsonObject, Tags.ALBUM_RELEASE_DATE));

		}

		catch (Exception e)

		{
			e.printStackTrace();

			return null;

		}

		return album;

	}

	private static Track buildTrack(JSONObject jsonObject) {

		Track track = new Track();

		try {
			track.setId(getString(jsonObject, TRACK_ID));
			track.setTitle(getString(jsonObject, Tags.TRACK_TITLE));
			track.setArtistString(getString(jsonObject, TRACK_ARTIST));
			track.setAlbumString(getString(jsonObject, TRACK_ALBUM));
			track.setLink(getString(jsonObject, TRACK_LINK));
			track.setTrack_position(getString(jsonObject, TRACK_POSITION));
			track.setDuration(getString(jsonObject, TRACK_DURATION));
			track.setRank(getString(jsonObject, TRACK_RANK));
			track.setPreview(getString(jsonObject, TRACK_PREVIEW));
			track.setReadable(getString(jsonObject, TRACK_READABLE));

		}

		catch (Exception e)

		{
			e.printStackTrace();

			return null;

		}

		return track;

	}

}